package vip

import (
	"context"
	"fmt"
	"net/http"

	"2106A-zg6/baidu/gateway/internal/svc"
	"2106A-zg6/baidu/gateway/internal/types"
	"2106A-zg6/baidu/srv/baiduclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserSorceLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserSorceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserSorceLogic {
	return &UserSorceLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserSorceLogic) UserSorce(req *types.UserVipCard) (resp *types.Response, err error) {

	value := l.ctx.Value("custom-key")

	_, err = l.svcCtx.BaiduSrv.CheckVipCar(l.ctx, &baiduclient.CheckVipCarRequest{
		VipCardId: req.VipCard,
	})
	if err != nil {
		return &types.Response{
			Msg:  err.Error(),
			Code: http.StatusAccepted,
			Data: nil,
		}, nil
	}

	vip, err := l.svcCtx.BaiduSrv.ScoreChangeVip(l.ctx, &baiduclient.ScoreChangeVipRequest{
		UserId:    fmt.Sprintf("%v", value),
		VipId:     "1",
		VipCardId: req.VipCard,
	})
	if err != nil {
		return nil, err
	}

	return &types.Response{
		Msg:  "success",
		Code: http.StatusOK,
		Data: vip,
	}, nil
}
